package com.skyline.user.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

//TODO:加注释
public class OnlineUsers {
	private static List<OnlineUser> onlineusers = new ArrayList<OnlineUser>();

	public static int getOnlineUserNum() {
		return onlineusers.size();
	}

	public static OnlineUser findUser(String sessionid) {
		for (OnlineUser u : onlineusers) {
			if (u.getSessionid().equals(sessionid))
				return u;
		}
		return null;
	}


	public static void login(String sessionid, Integer userid, String username) {
		OnlineUser user = findUser(sessionid);
		// System.out.println(u.getAttempt());
		user.setUserid(userid);
		user.setUsername(username);
		user.setLogintime(new Date());
	}

	public static void relogin(String sessionid, Integer userid, String username) {
		OnlineUser user = findUser(sessionid);
		user.setUserid(userid);
		user.setUsername(username);
		user.setLogintime(new Date());
	}

	public static void logout(String sessionid) {
		OnlineUser user = findUser(sessionid);
		onlineusers.remove(user);
	}

	public static List<OnlineUser> getOnlineUsers() {
		return onlineusers;
	}
	
	//TODO:重写toString()方法
}
